<?php

namespace App\Http\Controllers\Foundation;


use Illuminate\Http\Request;
use App\Model\System\Token;
use Laravel\Lumen\Routing\Controller as BaseController;


use App\Model\Foundation\CourseType;
use App\Http\Controllers\MessagePacket;


class CourseTypeController extends BaseController
{
    use MessagePacket;


    function  __construct(){}


    /**
     * 获取单一课程类型的信息
     * @param $id
     * @return packetMessage
     */
    function getCourseType($id)
    {
        $id = intval($id);
        $courseType = CourseType::find($id);
        if(empty($courseType)){
            return $this->fail($courseType, '数据不存在');
        }
        return $this->success($courseType);
    }


    /**
     * 批量查询学生列表
     * @param Request $request
     * @return packetMessage
     */
    function getCourseTypes(Request $request)
    {
        $name = '';
        if($request->has('coursetypename')) $name= $request->get('coursetypename');
        $courseType = CourseType::where('coursetypename', 'like', "%$name%");

        $data = $courseType->get();



        if(empty($data)){
            return $this->fail(false, '数据不存在');
        }
        return $this->success($data);
    }


    /**
     * 添加一个课程类型
     * @param Request $request
     * @return packetMessage
     */
    function  addCourseType(Request $request)
    {
        try{
            $courseType = new CourseType($request->all());
            $courseType->created_by = Token::user()->userid;
            $courseType->updated_by = Token::user()->userid;
            $courseType->save();
            return $this->success($courseType);
        }catch (\Exception $e){
            return $this->fail(false,'保存失败：'.$e->getMessage());
        }

    }


    /**
     * 更新某个课程类型的信息
     * @param Request $request
     * @return packetMessage
     */
    function  updateCourseType($id, Request $request)
    {
        try{
            if(empty($id)){
                if($request->has('coursetypeuid'))
                    $id = $request->get('coursetypeuid');
            }
            $courseType = CourseType::find($id);
            $courseType->update($request->all());
            $courseType->updated_by = Token::user()->userid;
            $courseType->save();
            return $this->success($courseType,"更新成功");
        }catch (\Exception $e){
            return $this->fail(false,'保存失败：'.$e->getMessage());
        }

    }


    /**
     * 删除某个学员
     * @param $id
     * @return packetMessage
     */
    function  deleteCourseType($id)
    {
        try{
            CourseType::destroy($id);
            return $this->success(true,"删除成功");
        }catch (\Exception $e){
            return $this->fail(false,'删除失败：'.$e->getMessage());
        }


    }

}
